package health.lm.com.home.adapter;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;

import java.util.List;

import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.data.App;
import health.lm.com.home.R;
import health.lm.com.home.entity.ContentItemEntity;

/**
 * @author GQI
 * @description 内容fragment适配器
 * @date :2019-07-18 16:34
 */
public class ContentItemAdapter extends BaseMultiItemQuickAdapter<ContentItemEntity, BaseViewHolder> {
    public final static String TAG = "TT4";
    private GSYVideoHelper smallVideoHelper;
    private GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;
    private ImageView imageView;
    //private ContentItemPresenter contentItemPresenter;
    private int pos;

    public ContentItemAdapter(List<ContentItemEntity> data/*, ContentItemPresenter contentItemPresenter*/) {
        super(data);
        /** 大图*/
        addItemType(ContentItemEntity.BIG_PIC, R.layout.item_content_big_pic);
        /** 图文*/
        addItemType(ContentItemEntity.IMAGE_TEXT, R.layout.item_content_image_text);
        /** 问答*/
        addItemType(ContentItemEntity.QUESTION, R.layout.item_content_qusetion);
        /** 视频*/
        addItemType(ContentItemEntity.VIDEO, R.layout.item_content_video);
        /** 三图片*/
        addItemType(ContentItemEntity.THREE_PIC, R.layout.item_content_three_pic);
        //this.contentItemPresenter = contentItemPresenter;
    }

    public void setVideoHelper(GSYVideoHelper smallVideoHelper, GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder) {
        this.smallVideoHelper = smallVideoHelper;
        this.gsySmallVideoHelperBuilder = gsySmallVideoHelperBuilder;
    }

    @Override
    protected void convert(BaseViewHolder helper, ContentItemEntity item) {
        /** 大图*/
        if (ContentItemEntity.BIG_PIC == helper.getItemViewType()) {
            helper.setText(R.id.tv_big_title, item.getTitle())
                    .addOnClickListener(R.id.iv_content_delete);
            /** 图文*/
        } else if (ContentItemEntity.IMAGE_TEXT == helper.getItemViewType()) {
            helper.setText(R.id.tv_content_title, item.getTitle())
                    .setText(R.id.tv_content_autor, item.getShow_views())
                    .setText(R.id.tv_content_reading, item.getReward())
                    .addOnClickListener(R.id.iv_content_delete);
            ImageLoaderHelper.getInstance().load(mContext, item.getImg_url(), helper.getView(R.id.iv_content_pic));
            ImageLoaderHelper.getInstance().load(mContext, App.getModel().getSugar_url(), helper.getView(R.id.iv_content_icon));
            /** 问答*/
        } else if (ContentItemEntity.QUESTION == helper.getItemViewType()) {
            /** 视频*/
        } else if (ContentItemEntity.VIDEO == helper.getItemViewType()) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            smallVideoHelper.addVideoPlayer(helper.getAdapterPosition(), imageView,
                    TAG, helper.getView(R.id.fl_video), helper.getView(R.id.iv_video_play));
            helper.getView(R.id.iv_video_play).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    smallVideoHelper.setPlayPositionAndTag(helper.getAdapterPosition(), TAG);
                    notifyDataSetChanged();
                    //listVideoUtil.setLoop(true);
                    //listVideoUtil.setCachePath(new File(FileUtils.getPath()));
                    //contentItemPresenter.isGet = true;
                    gsySmallVideoHelperBuilder.setUrl(item.getVideo_url());
                    smallVideoHelper.startPlay();
                    pos = helper.getLayoutPosition();
                    //contentItemPresenter.views(item.getNews_id());
                    //必须在startPlay之后设置才能生效
                    //listVideoUtil.getGsyVideoPlayer().getTitleTextView().setVisibility(View.VISIBLE);
                }
            });

            helper.setText(R.id.tv_video_title, item.getTitle())
                    .setText(R.id.tv_content_reading, item.getReward())
                    .setText(R.id.tv_video_plays, item.getShow_views());
            ImageLoaderHelper.getInstance().load(mContext, App.getModel().getSugar_url(), helper.getView(R.id.iv_content_icon));
            ImageLoaderHelper.getInstance().load(mContext, item.getImg_url(), imageView);
            /** 三图片*/
        } else if (ContentItemEntity.THREE_PIC == helper.getItemViewType()) {
        }
    }

    public int videoPos() {
        return pos;
    }
}
